<?php
/*
 * @description: Модель для работы с историей
 * @copyright Copyright (c) 2012-2017 Marin V. , Tykva A. , Kozel R. , Sokolov D.
 * @version 1.0
 * @link http://lecturebox.ru
*/

class Model_Timeline
{
    protected $lecture_id;

    protected $_dbTable;
    protected $_row;
    
    protected $data;

    public function __construct($lecture_id=0)
    {
        $this->lecture_id = $lecture_id;
        $this->data = null;

        $this->_dbTable = new Model_DbTable_Timeline();
        if($lecture_id)
            $this->_row = $this->_dbTable->find($lecture_id)->current();
        else
            $this->_row = $this->_dbTable->createRow();
    }
        
//    public function getSubjectsById($id)
//    {   
//        $select = $this->_dbTable->select();
//        $select->where('subject_id = ?', $id);
//        return $this->_row = $this->_dbTable->fetchAll($select);
//    }
    
    public function setTimeline($uid, $lid, $ltitle, $cname)
    {   
        $data = array(
            'title'      => $ltitle,
            'user_id'      => $uid,   
            'lecture_id'      => $lid,
            'course_name' => $cname,
            'rank'      => '0',      
            'date'      => date("Y-m-d H:i:s")
        ); 
        $this->_dbTable->insert($data);
    }
    
    public function setTimelineRank($rank, $user_id, $lecture_id)
    {   
        $data = array(
            'rank'      => $rank,
            'date'      => date("Y-m-d H:i:s")
        );

        //$where = $this->_dbTable->getAdapter()->quoteInto('lecture_id = ?', $lecture_id);
        
        $where = array(
            'user_id = ?' => $user_id,
            'lecture_id = ?' => $lecture_id
        );
        
        $this->_dbTable->update($data, $where);

    }
    
    public function updateTimelineDate($user_id, $lecture_id)
    {   
        $data = array(
            'date'      => date("Y-m-d H:i:s")
        );
        
        $where = array(
            'user_id = ?' => $user_id,
            'lecture_id = ?' => $lecture_id
        );
        
        $this->_dbTable->update($data, $where);
    }

    public function getTimelineByUser($uid)
    {   
        $select = $this->_dbTable->select();
        $select->where('user_id = ?', $uid);
        $select->order('date DESC');
        return $this->_row = $this->_dbTable->fetchAll($select);
    }
    
    public function checkTimeline($user_id, $lecture_id)
    {   
        $select = $this->_dbTable->select();
        $select->where('user_id = ?', $user_id)
                ->where('lecture_id =?', $lecture_id);
        return $this->_row = $this->_dbTable->fetchAll($select);
    }
    
    public function getTimelineByLecture($lecture_id)
    {   
        $select = $this->_dbTable->select();
        $select->where('lecture_id = ?', $lecture_id);
        return $this->_row = $this->_dbTable->fetchAll($select);
    }
    
    public function delete(){
        
        $this->_row->delete();
    }


    public function __set($name, $val)
    {
        if (isset($this->_row->$name)) {
            $this->_row->$name = $val;
        }
    }

    public function __get($name)
    {
        if (isset($this->_row->$name)) {
            return $this->_row->$name;
        }
    }
 
}
